// TileWnd.cpp : implementation file
//

#include "stdafx.h"
#include "T3_Editor.h"
#include "TileWnd.h"
#include "T3_EditorDoc.h"

// CTileWnd

IMPLEMENT_DYNAMIC(CTileWnd, CDockablePane)

CTileWnd::CTileWnd()
{

}

CTileWnd::~CTileWnd()
{
}


BEGIN_MESSAGE_MAP(CTileWnd, CDockablePane)
	ON_WM_CREATE()
	ON_WM_SIZE()
END_MESSAGE_MAP()

int CTileWnd::OnCreate( LPCREATESTRUCT lpCreateStruct )
{
	CDockablePane::OnCreate( lpCreateStruct );

	CRect rect;
	GetClientRect(&rect);
	m_lstTileIcon.Create( WS_CHILD |
						  WS_VISIBLE |
						  LVS_AUTOARRANGE |			// Re-arrange icons after resize
						  LVS_SHOWSELALWAYS |		// Keep selected item shown when control lost focus
						  LVS_SINGLESEL,			// Only one item at a time can be selected
						  rect, this, ID_VIEW_TILEICON_LIST);

	m_lstTileIcon.Arrange( LVA_ALIGNTOP );

	//CBitmap bitmap;
	//bitmap.

	m_ImageList.Create(32, 32, ILC_COLOR32, 0, 1);
	m_ImageList.Add( AfxGetApp()->LoadIcon(IDR_MAINFRAME) );

	m_lstTileIcon.SetImageList( &m_ImageList, LVSIL_NORMAL );

	return TRUE;
}

void CTileWnd::OnSize( UINT nType, int cx, int cy )
{
	CRect rect;
	GetClientRect( &rect );

	m_lstTileIcon.MoveWindow( &rect );
}


void CTileWnd::UpdateTileList()
{
	m_lstTileIcon.DeleteAllItems();

	CFrameWnd* frameWnd = (CFrameWnd*)AfxGetMainWnd();
	if (!frameWnd) return;

	CView* view = frameWnd->GetActiveView();
	if (!view) return;

	CT3_EditorDoc* doc = (CT3_EditorDoc*) view->GetDocument();
	if (!doc) return;

	GameStage* stage = doc->GetGameStage();

	LVITEM lvItem;

	for (int i=0; i<stage->GetTileCount(); i++)
	{
		CString tile_name = CharToCString( stage->GetTileNameById(i) );

		lvItem.mask = LVIF_TEXT | LVIF_IMAGE;
		lvItem.iItem = i;
		lvItem.iSubItem = 0;
		lvItem.pszText = tile_name.GetBuffer();
		lvItem.iImage = 0;
		m_lstTileIcon.InsertItem( &lvItem );

		tile_name.ReleaseBuffer();
	}
}

BOOL CTileWnd::GetSelectedTileName( char* tileName )
{
	POSITION pos = m_lstTileIcon.GetFirstSelectedItemPosition();

	while (pos)
	{
		int nItem = m_lstTileIcon.GetNextSelectedItem( pos );

		CString strTileName = m_lstTileIcon.GetItemText(nItem, 0);
		return CStringToChar( tileName, strTileName );
	}

	return FALSE;
}



// CTileWnd message handlers


